package com.zjq.authoritysystem.utils;

import com.alibaba.fastjson.JSON;
import com.zjq.authoritysystem.entity.Permission;
import com.zjq.authoritysystem.vo.RouteVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.parameters.P;

import javax.swing.text.html.Option;
import java.util.*;

/**
 * @author Kimiaaaaa
 * @className MenuTreeUtil
 * @Description 菜单树工具类
 * @date 2023-09-21 16:57
 */
@Slf4j
public class MenuTreeUtil {

    /**
     * 生成路由
     *
     * @param menuList 菜单列表
     * @param pid      父菜单编号
     * @return 路由信息
     */
    public static List<RouteVo> makeRoute(List<Permission> menuList, Integer pid) {
        //创建集合保存路由信息
        List<RouteVo> routeVos = new ArrayList<>();
        //判断当前菜单列表是否为空，如果不为空则使用菜单列表，否则创建集合对象
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                //筛选不为空的菜单以及与菜单父ID相同的数据
                .stream().filter(permission -> permission != null && permission.getParentId() == pid)
                .forEach(permission -> {
                    //创建路由信息对象
                    RouteVo routeVo = new RouteVo();
                    routeVo.setName(permission.getName());
                    routeVo.setPath(permission.getPath());
                    //判断当前菜单是否是一级菜单
                    if (permission.getParentId() == Permission.PARENT_MENU_ID) {
                        routeVo.setComponent("Layout"); //一级菜单组件
                        routeVo.setAlwaysShow(true); //显示路由
                    } else {
                        routeVo.setComponent(permission.getUrl()); //具体组件地址
                        routeVo.setAlwaysShow(false); //折叠路由
                    }
                    //设置meta信息
                    routeVo.setMeta(routeVo.new Meta(permission.getLabel(), permission.getIcon(), permission.getCode().split(",")));
                    //递归生成路由(子菜单)
                    List<RouteVo> children = makeRoute(menuList, permission.getId());
                    //设置子路由
                    routeVo.setChildren(children);
                    routeVos.add(routeVo);
                });


        return routeVos;
    }

    /**
     * 生成菜单树
     *
     * @param menuList 菜单列表
     * @param pid      父菜单编号
     * @return 菜单树
     */
    public static List<Permission> makeMenuTree(List<Permission> menuList, Integer pid) {
        List<Permission> permissionList = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream().filter(permission -> permission != null && permission.getParentId() == pid)
                .forEach(permission -> {
                    //创建权限菜单对象
                    Permission newPermission = new Permission();
                    //将permission的属性值复制到newPermission中
                    BeanUtils.copyProperties(permission, newPermission);
                    //递归生成菜单树
                    List<Permission> children = makeMenuTree(menuList, permission.getId());
                    //设置子菜单
                    if (children.size() > 0) {
                        newPermission.setChildren(children);
                    }
                    permissionList.add(newPermission);
                });
        return permissionList;
    }
}
